/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const ArticleHrefAdapter = require('../ArticleHrefAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const cheerio = require('cheerio');
const urlTool = require('url');
const urldecode = require('urlencode')

/**
 * 中国证监会-信息披露
 * @type {module.Bjp2p}
 */
module.exports = class XinHuaZhongWen extends ArticleHrefAdapter {


    parseHtml(html, linkRule) {
        //将解析后的内容加载进解析器
        let $ = cheerio.load(html);
        let table = $(".content_table").children().eq(0).children().eq(0).children().eq(0).html();
        let contents = [];
        let that = this;
        let rangeText = '';
        let i = 0;
        let encoding = this.encoding;
        $ = cheerio.load(table);
        $("tr").each(function () {
            if(i==0 || i>20) {
                i++
            } else {
                i++
                let title = $(this).children().eq(0).text().trim();
                title += " " + $(this).children().eq(1).text().trim();
                let href = $(this).children().eq(5).children().eq(0).attr('href');
                if(href){
                    href = href.substring(0,18) + href.substring(62,href.length)
                }
                href = "http://ipo.csrc.gov.cn/" + href;
                if(!/\([0-9]+\)/g.test(title) && !/（[0-9]+）/g.test(title)) {

                    if (title.length != 0 || href.length != 0) {
                        contents.push({
                            title: title,
                            href: href
                        });
                        rangeText += title + href;
                    }
                }
            }



        });

        return {
            rangeText: rangeText,
            contents: contents
        };
    }


}
